/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.tag;

import com.servengine.portal.Portal;

import java.util.Date;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@NamedQueries ({
	@NamedQuery(name = "TaggedItem.findByModuleType", query = "select o from TaggedItem o where o.portal.id = :portal and o.module = :module and o.itemType = :itemType"),
	@NamedQuery(name = "TaggedItem.findByModuleTypeAndItemId", query = "select o from TaggedItem o where o.portal.id = :portalid and o.module = :module and o.itemType = :itemType and o.itemId = :itemId order by o.date desc"),
	@NamedQuery(name = "TaggedItem.findByModuleTypeAndItemName", query = "select o from TaggedItem o where o.portal.id = :portalid and o.module = :module and o.itemType = :itemType and o.itemName = :itemName order by o.date desc"),
	@NamedQuery(name = "TaggedItem.findByModuleTypeAndTag", query = "select o from TaggedItem o where o.portal.id = :portalid and o.module = :module and o.itemType = :itemType and o.tag = :tag order by o.date desc")
})
@Table (uniqueConstraints = {@UniqueConstraint(columnNames={"portal_id","module","itemType","itemName","itemId","tag_body"})})
public class TaggedItem implements Serializable,Comparable<TaggedItem>
{
	java.util.Date date;
	Portal portal;
	String module,itemType,itemName;
	Integer id,itemId;
	Tag tag;
	
	public TaggedItem() {}

	public TaggedItem(Portal portal, String module, String itemType, Integer itemId, Tag tag)
	{
		this.date=new java.util.Date();
	  this.portal=portal;
	  this.module=module;
	  this.itemType=itemType;
//	  this.itemId=itemId;
	  this.itemId=itemId;
	  this.tag=tag;
	}
	
	public int compareTo(TaggedItem item)
	{
		int result = date.compareTo(item.getDate());
		return result==0 ? id.compareTo(item.getId()) : result;
	}

	public boolean equals (Object item)
	{
		return ((TaggedItem)item).getId()==getId();
	}

	public void setDate(Date date)
	{
		this.date = date;
	}

	public Date getDate()
	{
		return date;
	}

	public void setPortal(Portal portal)
	{
		this.portal = portal;
	}

	@ManyToOne(fetch=FetchType.EAGER, optional=false)
	public Portal getPortal()
	{
		return portal;
	}

	public void setModule(String module)
	{
		this.module = module;
	}

	public String getModule()
	{
		return module;
	}

	public void setItemType(String itemType)
	{
		this.itemType = itemType;
	}

	public String getItemType()
	{
		return itemType;
	}

	public void setItemName(String itemId)
	{
		this.itemName = itemId;
	}

	public String getItemName()
	{
		return itemName;
	}

	@Id @GeneratedValue
	public void setId(Integer id)
	{
		this.id = id;
	}

	@Id @GeneratedValue
	public Integer getId()
	{
		return id;
	}

	public void setItemId(Integer itemId)
	{
		this.itemId = itemId;
	}

	public Integer getItemId()
	{
		return itemId;
	}

	public void setTag(Tag tag)
	{
		this.tag = tag;
	}

	@ManyToOne(fetch=FetchType.EAGER,optional=false)
	public Tag getTag()
	{
		return tag;
	}
}
